import { PaginationParam, PaginationResult } from "../../models";
import { LineTrendItem } from "../projectManage/models";
import type {
  EqPropListResult,
  EquipmentGroupItem,
  EquipmentGroupList,
  EquipmentItem,
  EquipmentPropResult,
  EquipmentStatusItem,
  EquipmentTypeItem,
  EquipmentTypeListItem,
  HealthInfo,
  ParEquipmentDetail,
  SearchGroupTypeItem,
  ServiceRecordItem,
  ServiceTypeCountItem,
} from "./models";
import req, { RequsetOptons } from "/@/api/req";
export enum Api {
  addEquipment = "api/aiot/equipment/addEquipment",
  deleteEquipmentByIds = "api/aiot/equipment/deleteEquipmentByIds",
  // getEquipmentByEquipmentId = "api/aiot/equipment/getEquipmentByEquipmentId",
  updateEquipment = "api/aiot/equipment/updateEquipment",
  pageForCustomer = "api/aiot/equipment/pageForCustomer",
  pageEquipments = "api/aiot/equipment/page",
  // getEquipmentById = "api/aiot/equipment/getEquipmentById",
  pageEquipmentType = "api/aiot/equipmentType/pageEquipmentType",
  getEquipmentProperty = "api/aiot/equipment/getEquipmentPropertyById",
  getEquipmentPropertyBatch = "api/aiot/equipment/getEquipmentPropertyBatch",
  getEquipmentPropertyList = "api/aiot/equipment/getEquipmentPropertyList",
  getEquipmentStatusBatch = "api/aiot/equipment/getEquipmentStatusBatch",
  pageEquipmentByType = "api/v1/equipments/group_page",
  pageEquipmentTypes = "api/v1/equipments/group_list",
  pageEquipmentGroups = "api/aiot/equipmentGroups/pageEquipmentGroups",
  equipmentTrends = "api/v1/equipments/energies/trends",
  serviceRecordPage = "api/v1/equipments/service/page",
  serviceTypeCount = "api/v1/equipments/service/count",
  systemCountSummary = "api/aiot/iotBoards/systemCountSummary",
  updateEquipmentBatch = "api/aiot/equipment/updateEquipmentBatch",

  getEquipmentDetail = "api/aiot/equipment/getEquipmentDetail",
  getEquipmentAttrValueById = "api/aiot/equipment/getEquipmentAttrValueById",
  getEquipmentHealInfo = "api/v1/equipments/getEquipmentHealInfo",
}

// 服务记录
export function serviceTypeCount(data: { equipmentId?: string; workType?: string; searchText?: string }) {
  return req.post<Array<ServiceTypeCountItem>>(
    {
      url: Api.serviceTypeCount,
      data,
    },
    {
      useGlobalLoading: false,
    }
  );
}
// 服务记录
export function serviceRecordPage(data: { equipmentId?: string; workType?: string }) {
  return req.post<ServiceRecordItem>(
    {
      url: Api.serviceRecordPage,
      data,
    },
    {
      useGlobalLoading: false,
    }
  );
}

// 设备用电趋势
export function equipmentTrends(data: { equipmentId?: string; period?: string }) {
  return req.post<LineTrendItem>(
    {
      url: Api.equipmentTrends,
      data,
    },
    {
      useGlobalLoading: false,
    }
  );
}
// 类型分组-设备列表
export function pageEquipmentByType(data: PaginationParam<SearchGroupTypeItem>) {
  return req.post<PaginationResult<EquipmentItem>>(
    {
      url: Api.pageEquipmentByType,
      data,
    },
    {
      useGlobalLoading: false,
    }
  );
}
// 类型分组-设备列表
export function pageEquipmentTypes(data: PaginationParam<RequsetOptons>) {
  return req.post<EquipmentGroupList<EquipmentTypeListItem>>(
    {
      url: Api.pageEquipmentTypes,
      data,
    },
    {
      useGlobalLoading: false,
    }
  );
}
// 获取设备分组
export function pageEquipmentGroups(projectId?: string) {
  return req.post<PaginationResult<EquipmentGroupItem>>(
    {
      url: Api.pageEquipmentGroups,
      data: {
        current: 1,
        size: 999,
        model: {
          projectId,
        },
      },
    },
    {
      useGlobalLoading: false,
    }
  );
}

// 获取设备类型列表
export function pageEquipmentType(projectId?: string) {
  return req.post<PaginationResult<EquipmentTypeItem>>(
    {
      url: Api.pageEquipmentType,
      data: {
        current: 1,
        size: 999,
        model: {
          projectId,
        },
      },
    },
    {
      useGlobalLoading: false,
    }
  );
}

// export function getEquipmentById(id: string) {
//   return req.post<ParEquipmentDetail>(
//     {
//       url: Api.getEquipmentByEquipmentId,
//       params: {
//         equipmentId: id,
//       },
//     },
//     {
//       useGlobalLoading: false,
//     }
//   );
// }

export function getEquipmentDetail(equipmentId: string) {
  return req.commonPost<ParEquipmentDetail>(
    Api.getEquipmentDetail + "?equipmentId=" + equipmentId,
    { equipmentId },
    { useGlobalLoading: false }
  );
}

export function updateEquipment(data: ParEquipmentDetail) {
  return req.post<boolean>({
    url: Api.updateEquipment,
    data,
  });
}

export function getEquipmentProperty(equipmentId: string, loading = true) {
  return req.commonGet<EquipmentPropResult>(Api.getEquipmentProperty, { equipmentId }, { useGlobalLoading: loading });
}

export function getEquipmentPropertyBatch(equipmentIds: string[]) {
  return req.commonPost<EquipmentPropResult[]>(
    Api.getEquipmentPropertyBatch,
    { equipmentIds },
    { useGlobalLoading: false, useDefaultErrHandle: false, useDefaultFailHandle: false }
  );
}

export function getEquipmentStatusBatch(equipmentIds: string[]) {
  return req.commonPost<EquipmentStatusItem[]>(
    Api.getEquipmentStatusBatch,
    { equipmentIds },
    { useGlobalLoading: false, useDefaultErrHandle: false, useDefaultFailHandle: false }
  );
}

export function getEquipmentPropertyList(equipmentId: string, loading = true) {
  return req.commonPost<EqPropListResult[]>(
    Api.getEquipmentPropertyList,
    { equipmentIds: [equipmentId] },
    { useGlobalLoading: loading }
  );
}

export function systemCountSummary(projectId: string) {
  return req.commonPost<EquipmentPropResult>(
    Api.systemCountSummary,
    { projectId, queryType: "ldz" },
    { useGlobalLoading: false }
  );
}
export function addEquipment(data: ParEquipmentDetail) {
  return req.commonPost<ParEquipmentDetail>(Api.addEquipment, data);
}
export function updateEquipmentBatch(data: Array<ParEquipmentDetail>) {
  return req.commonPost<boolean>(Api.updateEquipmentBatch, data);
}
export function getEquipmentHealInfo(equipmentId: string) {
  return req.commonPost<HealthInfo>(Api.getEquipmentHealInfo, { equipmentId });
}

export function getEquipmentAttrValueById(equipmentId: string) {
  return req.commonPost<EquipmentItem["equipmentParamDto"]>(
    Api.getEquipmentAttrValueById + "?equipmentId=" + equipmentId,
    { equipmentId },
    { useGlobalLoading: false }
  );
}
